package OnlineLibrary.managers;

import java.util.ArrayList;

import Common.Customer;
import OnlineLibrary.NotificationsBuyingMgr_Iface.NotificationBuyingMgrPOA;
import Util.CSVBookParser;
import Util.Util;

/**
 * Manages auction performed by book store and the books returned by the customers
 *
 */
public class NotificationBuyingMgr extends NotificationBuyingMgrPOA {
	private DeliveryMgr deliveryMgr;

	public NotificationBuyingMgr(DeliveryMgr deliveryMgr) {
		this.deliveryMgr = deliveryMgr;
	}

	@Override
	public void notifCreditNote(int sellingNumber) {
		System.out.println("new credit note for selling nr :" + sellingNumber);
		CSVBookParser.writeFile("" + sellingNumber,
				Util.getPropertiesAttribute(Mgr.getPropertiesFile(), "return"));

	}

	@Override
	public void completeAuction(int sellingNumber, float price, String BSId) {
		System.out.println("new complete auction:" + sellingNumber + "=>"
				+ price);
		CSVBookParser.writeFile(sellingNumber + ";" + price + ";" + BSId,
				Util.getPropertiesAttribute(Mgr.getPropertiesFile(), "csv2"));
		System.out.println("auction recorded");
		CSVBookParser parser = new CSVBookParser();
		ArrayList<String> list = parser.getContent(Util.getPropertiesAttribute(
				Mgr.getPropertiesFile(), "csv"));
		boolean found = false;
		String record = "";
		//check if the identifier of the completed auction is already present in the csv file
		for (int i = 0; i < list.size() && !found; i++) {
			record = list.get(i);
			if (record.split(";")[0].equals("" + sellingNumber))
				found = true;

		}
		if (!found)//problem
			System.out.println("Selling not found!");
		else {
			//call the parcel delivery to start the book delivery
			String[] l = record.split(";");
			
			
			
			String isbn = l[1];
			Customer cust = new Customer(l[3], l[4], l[5], l[6], l[7], l[8],
					l[9], l[10]);
			System.out.println("isbn" + isbn);
			System.out.println("sellingnr" + sellingNumber);
			System.out.println("BSId" + BSId);
			deliveryMgr.newDelivery(sellingNumber, isbn, cust, BSId);
			System.out.println("selling book sent to parcel delivery");
		}

	}

}
